#!/usr/bin/env node
const inquirer = require('inquirer')
const path = require('path')
const fs = require('fs')
const ejs = require('ejs')
const { formatWithOptions } = require('util')

function readfiles(dir,callback){
    fs.readdir(dir,{withFileTypes:true},(err,files)=>{
        files.forEach((value,index)=>{
          if(value.isDirectory()){
              readfiles(path.join(dir,value.name),callback)
          }  else{
              callback(path.join(dir,value.name))
          }
        })
    })
}


 inquirer.prompt([
     {
         type:'input',
         name:'web_name',
         default:path.basename(__dirname)
     },
     {
         type:'input',
         name:'path',
         default: 'src',
         message:'which path you put it in'
     }
 ]).then(answers=>{
    let tplPath = path.join(__dirname,'./templates')
    let distPath = path.join(process.cwd(),answers.path)
    
    readfiles(tplPath,function(filePath){
        let res = fs.mkdirSync(
            path.dirname(path.join(distPath,filePath.replace(tplPath,''))),
            {recursive:true},
        )
        fs.writeFileSync(
            path.join(distPath,filePath.replace(tplPath,'')),
            ejs.render(fs.readFileSync(filePath,'utf-8'),answers),
            {flag:'w+'}
        )
    })
 })
 

